package org.androidpn.server.console.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.androidpn.server.exception.UserExistsException;
import org.androidpn.server.model.Message;
import org.androidpn.server.model.User;
import org.androidpn.server.service.ServiceLocator;
import org.androidpn.server.service.UserService;
import org.androidpn.server.util.Config;
import org.androidpn.server.xmpp.push.NotificationManager;
import org.message.service.MessageService;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * android��Ϣ����Controller��
 *
 * @����   ������
 * @�������� 2016��3��1��
 *
 */
public class NotificationController extends MultiActionController {

	private NotificationManager notificationManager;
	private MessageService messageService;
	private UserService userService;

	public NotificationController() {
		notificationManager = new NotificationManager();
		messageService=ServiceLocator.getMessageervice();
		userService=ServiceLocator.getUserService();
	}

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("notification/form");
		return mav;
	}

	public ModelAndView send(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String broadcast = ServletRequestUtils.getStringParameter(request,"broadcast", "Y");
		String username = ServletRequestUtils.getStringParameter(request,"username");
		String title = ServletRequestUtils.getStringParameter(request, "title");
		String message = ServletRequestUtils.getStringParameter(request,"message");
		String uri = ServletRequestUtils.getStringParameter(request, "uri");

		String apiKey = Config.getString("apiKey", "");
		logger.debug("apiKey=" + apiKey);

		if (broadcast.equalsIgnoreCase("Y")) { //�����豸
			notificationManager.sendBroadcast(apiKey, title, message, uri);
			
			List<User> list=userService.getUsers();
			for(User user:list){
				 saveMessage(user.getId(),message,uri,0);
			}
		} else { //�����豸
			notificationManager.sendNotifcationToUser(apiKey, username, title,message, uri);
		    User user=userService.getUserByUsername(username);
		    saveMessage(user.getId(),message,uri,0);
		}
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:notification.do");
		return mav;
	}
	@RequestMapping("send1")
	public void send1(HttpServletRequest request,HttpServletResponse response) throws Exception {
		System.out.println(11111111+"-----------------------------------------------");
		request.setCharacterEncoding("utf-8");
		String broadcast = ServletRequestUtils.getStringParameter(request,"broadcast", "Y");
		String username = ServletRequestUtils.getStringParameter(request,"username");
		String title = ServletRequestUtils.getStringParameter(request, "title");
		String message = ServletRequestUtils.getStringParameter(request,"message");
		System.out.println(message);
		String uri = "http://mytest.ss360.org:8080/frame/manage/findNewsInfo.action?news_id="+ServletRequestUtils.getStringParameter(request, "uri");
		String apiKey = Config.getString("apiKey", "");
		logger.debug("apiKey=" + apiKey);
		
		if (broadcast.equalsIgnoreCase("Y")) { //�����豸
			notificationManager.sendBroadcast(apiKey, title, message, uri);
			
			List<User> list=userService.getUsers();
			for(User user:list){
				 saveMessage(user.getId(),message,uri,0);
			}
		} else { //�����豸
			notificationManager.sendNotifcationToUser(apiKey, username, title,message, uri);
		    User user=userService.getUserByUsername(username);
		    saveMessage(user.getId(),message,uri,0);
		}
		
		/*ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:notification.do");*/
		response.getWriter().print("");
	}
	
	/**
	 * ���淢�͵���Ϣ��Ϣ
	 *
	 * @����   ������
	 * @�������� 2016��2��29��
	 *
	 * @param userId  �û�id
	 * @param content ��Ϣ����
	 * @param state   ����״̬
	 * @throws UserExistsException
	 * void
	 *
	 */
	private void saveMessage(Long userId,String content,String uri,int state) throws UserExistsException{
		Message msg=new Message();
		msg.setUserId(userId);
		msg.setContent(content);
		msg.setUri(uri);
		msg.setSendState(state); 
		
		messageService.saveMes(msg);
	}

}
